/**
 * This code is part of the Bumpslide Library by David Knape
 * http://bumpslide.com/
 * 
 * Copyright (c) 2006, 2007, 2008 by Bumpslide, Inc.
 * 
 * Released under the open-source MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * see LICENSE.txt for full license terms
 */
 package {
	import flash.text.AntiAliasType;	
	import flash.text.TextFormat;	
	
	import com.bumpslide.ui.TextBox;	
	import com.bumpslide.util.LoremIpsum;	
	import com.bumpslide.tween.FTween;	
	
	import flash.utils.getTimer;	
	import flash.events.TimerEvent;	
	
	import com.bumpslide.data.Binding;
	import com.bumpslide.ui.Applet;
	import com.bumpslide.ui.GenericButton;
	import com.bumpslide.ui.Panel;
	
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.utils.Timer;		

	/**
	 * Panel that autosizes to content 
	 * 
	 * @mxmlc -sp+=../../classes -debug=true -default-frame-rate=31
	 * @author David Knape
	 */
	public class Demo_08_AutoSizingPanel extends Applet {

		private var panel:Panel;
		private var image:Loader;
		private var autoSizeToggle:GenericButton;
		private var timer:Timer;
		
		[Embed(systemFont="Helvetica Neue", fontName="my_font", mimeType="application/x-font")]
		public var myFont:Class;
		
		override protected function addChildren():void {
			
			// create panel with x=10, y=10
			panel = new Panel();
			panel.move(10, 40);

			textbox = new TextBox( LoremIpsum.TEXT.split('\n')[0], new TextFormat('my_font'));
			textbox.content_txt.embedFonts = true;
			panel.content = textbox;
			panel.width = 510;
			
			
			// show the panel
			addChild(panel);
			
			// create button to demo auto-sizing
			// this is also a nice example of how bumpslide binding 
			// can be used for things other than ModelLocators 
			// note, Button (base class for GenericButton) dispatches
			// model change events (like flex prop change events)
			autoSizeToggle = new GenericButton( 200, 20, 10, 10);
			autoSizeToggle.toggle = true;
			
			Binding.create( autoSizeToggle, 'selected', this, function():void { 
				panel.autoSizeHeight = autoSizeToggle.selected;
				autoSizeToggle.label =  autoSizeToggle.selected ? "Auto-Size Height ON" : "Auto-Size Height OFF";
			} );
			
			
			addChild(autoSizeToggle);
			autoSizeToggle.selected = true;
			
			timer = new Timer( 30, 0);
			timer.addEventListener( TimerEvent.TIMER, animate);
			timer.start();
		}
		
		private var textbox:TextBox;
		private var frame:Number=0;
		
		private function animate(event:TimerEvent):void {
			var tf:TextFormat = textbox.textFormat;
			frame+=.03;
			tf.size = 12 + 16 * (Math.sin(frame)+1);
			textbox.textFormat = tf; 
			panel.updateNow();
		}
		

		// on stage resize, update the panel size
		override protected function draw():void {
			panel.setSize( width - 20, height - 50);
			super.draw();			
		}
	}
}
